package com.sykj.why.controller.admin.menu;

import com.google.gson.JsonArray;
import com.google.gson.JsonObject;
import com.sykj.why.exception.WhyException;
import com.sykj.why.handler.Response;
import com.sykj.why.mongo.document.entity.SystemMenuT;
import com.sykj.why.mongo.enums.exception.ResultEnum;
import com.sykj.why.mongo.service.SystemMenuTService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @author Eleven
 * @date 2019/6/5
 * @desc 后台系统菜单管理控制访问层
 */

@RestController
@RequestMapping("/admin/systemMenu")
@Slf4j
@Api(value = "/admin/systemMenu",description = "后台系统菜单管理控制访问层")
public class SystemMenuTController {

    @Autowired
    private SystemMenuTService systemMenuTService;

    /**
     * 添加系统菜单
     * @param systemMenuT
     * @return
     */
    @RequestMapping(value = "/add",method = RequestMethod.POST)
    @ApiOperation(value = "/add",notes = "添加系统菜单")
    public ResponseEntity add(@RequestBody SystemMenuT systemMenuT){
        try {
            systemMenuTService.saveSystemMenu(systemMenuT);
            return Response.success("添加成功");
        } catch (WhyException e) {
            return Response.serverError(e.getMessage(), e.getCode());
        } catch (Exception e) {
            log.error("添加系统菜单出现未知异常，{}", e.getMessage());
            return Response.serverError(ResultEnum.UNERROR.getMessage(), ResultEnum.UNERROR.getCode());
        }
    }

    /**
     * 删除系统菜单
     * @param id
     * @return
     */
    @RequestMapping(value = "/remove/{id}",method = RequestMethod.GET)
    @ApiOperation(value = "/remove",notes = "删除系统菜单")
    public ResponseEntity remove(@PathVariable String id){
        try {
            systemMenuTService.removeSystemMenu(id);
            return Response.success("删除成功");
        } catch (WhyException e) {
            return Response.serverError(e.getMessage(), e.getCode());
        } catch (Exception e) {
            log.error("删除系统菜单出现未知异常，{}", e.getMessage());
            return Response.serverError(ResultEnum.UNERROR.getMessage(), ResultEnum.UNERROR.getCode());
        }
    }

    /**
     * 修改系统菜单
     * @param systemMenuT
     * @return
     */
    @RequestMapping(value = "/update",method = RequestMethod.POST)
    @ApiOperation(value = "/update",notes = "修改系统菜单")
    public ResponseEntity update(@RequestBody SystemMenuT systemMenuT){
        try {
            systemMenuTService.updateSystemMenu(systemMenuT);
            return Response.success("修改成功");
        } catch (WhyException e) {
            return Response.serverError(e.getMessage(), e.getCode());
        } catch (Exception e) {
            log.error("修改系统菜单出现未知异常，{}", e.getMessage());
            return Response.serverError(ResultEnum.UNERROR.getMessage(), ResultEnum.UNERROR.getCode());
        }
    }

    /**
     * 启用/禁用系统菜单
     * @param id
     * @param flag
     * @return
     */
    @RequestMapping(value = "/enable",method = RequestMethod.POST)
    @ApiOperation(value = "/enable",notes = "启用/禁用系统菜单")
    public ResponseEntity enable(@RequestParam String id,@RequestParam boolean flag){
        try {
            systemMenuTService.disableOrEnable(id, flag);
            return Response.success("操作成功");
        } catch (WhyException e) {
            return Response.serverError(e.getMessage(), e.getCode());
        } catch (Exception e) {
            log.error("启用/禁用系统菜单出现异常，{}", e.getMessage());
            return Response.serverError(ResultEnum.UNERROR.getMessage(), ResultEnum.UNERROR.getCode());
        }
    }

    /**
     * 通过Id查找系统菜单
     * @param id
     * @return
     */
    @RequestMapping(value = "/findSystemMenuById/{id}",method = RequestMethod.GET)
    @ApiOperation(value = "/findSystemMenuById/{id}",notes = "通过Id查找系统菜单")
    public ResponseEntity findSystemMenuById(@PathVariable String id){
        try {
            SystemMenuT systemMenuT = systemMenuTService.findById(id);
            return Response.success("查询成功",systemMenuT);
        } catch (WhyException e) {
            return Response.serverError(e.getMessage(), e.getCode());
        } catch (Exception e) {
            log.error("通过Id查找系统菜单出现未知异常，{}", e.getMessage());
            return Response.serverError(ResultEnum.UNERROR.getMessage(), ResultEnum.UNERROR.getCode());
        }
    }

    /**
     * 查询全部系统菜单
     *
     * @return 全部系统菜单
     */
    @RequestMapping(value = "/findAll",method = RequestMethod.GET)
    @ApiOperation(value ="/findAll",notes = "查询全部系统菜单")
    public ResponseEntity findAll() {
        try {
            return Response.success("查询成功", systemMenuTService.findAll());
        } catch (WhyException e) {
            return Response.serverError(e.getMessage(), e.getCode());
        } catch (Exception e) {
            log.error("查询全部系统菜单出现异常，{}", e.getMessage());
            return Response.serverError(ResultEnum.UNERROR.getMessage(), ResultEnum.UNERROR.getCode());
        }
    }

    @RequestMapping(value = "/loadCheckMenuInfo",method = RequestMethod.POST)
    @ApiOperation(value = "/loadCheckMenuInfo")
    public ResponseEntity loadCheckMenuInfo(@RequestParam String pid){
        try {
            String json = getAllMenuByParentId(pid).toString();
            return Response.success("查询成功",json);
        } catch (WhyException e) {
            return Response.serverError(e.getMessage(), e.getCode());
        } catch (Exception e) {
            log.error("查询全部系统菜单出现异常，{}", e.getMessage());
            return Response.serverError(ResultEnum.UNERROR.getMessage(), ResultEnum.UNERROR.getCode());
        }
    }

    private JsonArray getAllMenuByParentId(String pid) {
        JsonArray jsonArray = this.getMenuByParentId(pid);
        for (int i = 0; i < jsonArray.size(); i++) {
            JsonObject jsonObject = (JsonObject) jsonArray.get(i);
            //判断该节点下时候还有子节点
            List<SystemMenuT> systemMenuTList = systemMenuTService.getSystemMenuListByPid(pid);
            if (systemMenuTList.size()==0) {
                continue;
            } else {
                jsonObject.add("children", getAllMenuByParentId(jsonObject.get("id").getAsString()));
            }
        }
        return jsonArray;
    }


    private JsonArray getMenuByParentId(String pid) {
        List<SystemMenuT> systemMenuTList = systemMenuTService.getSystemMenuListByPid(pid);
        JsonArray jsonArray = new JsonArray();
        for (SystemMenuT menu : systemMenuTList) {
            JsonObject jsonObject = new JsonObject();
            String menuId = menu.getId();
            jsonObject.addProperty("id", menuId); // 节点id
            jsonObject.addProperty("name", menu.getName()); // 节点名称
            //判断该节点下是否还有子节点
            List<SystemMenuT> menuTList = systemMenuTService.getSystemMenuListByPid(pid);
            if (menuTList.size()==0) {
                jsonObject.addProperty("open", "false"); // 无子节点
            } else {
                jsonObject.addProperty("open", "true"); // 有子节点
            }
            jsonObject.addProperty("state", String.valueOf(menu.getState()));
            jsonObject.addProperty("iconValue", menu.getIcon());
            jsonObject.addProperty("pId", menu.getPid());
            jsonArray.add(jsonObject);
        }
        return jsonArray;
    }

}
